
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.FAQListItem;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

/**
 * 描述:获取帮助列表请求类
 *
 * @author jamywang
 * @since 2015年11月13日 上午10:38:07
 */
public class FAQListTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * 获取帮助列表接口
     */
    public FAQListHttpResponse request() {

        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        // 生成请求包
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        // 生成响应包
        ResponsePackage resp = new FAQListResponsePackage();
        // 生成响应类
        FAQListHttpResponse response = new FAQListHttpResponse();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_FAQ_LIST);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_FAQ_LIST);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(response);
            return response;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    class FAQListResponsePackage extends HttpBaseResponsePackage<FAQListHttpResponse> {

        @Override
        protected void handleResponse(FAQListHttpResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "FAQListTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    response.setCode(jsonResp.optString("code"));
                    response.setMessage(jsonResp.optString("message"));
                    response.setPrompt(jsonResp.optString("prompt"));
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_FAQ_LIST.equalsIgnoreCase(cmd)) {
                        ArrayList<FAQListItem> itemList = new ArrayList<FAQListItem>();
                        JSONArray jsonResult = jsonResp.getJSONArray("data");
                        if (jsonResult != null) {
                            for (int j = 0; j < jsonResult.length(); j++) {
                                JSONObject o = jsonResult.getJSONObject(j);
                                FAQListItem item = new FAQListItem();
                                item.setItemId(o.getInt("id"));
                                item.setTitle(o.getString("title"));
                                if (!o.isNull("inserttime")) {
                                    item.setInsertTime(o.getInt("inserttime"));
                                }
                                itemList.add(item);
                            }
                            response.setFAQList(itemList);
                        }
                    }
                }
                response.setOk(true);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    public class FAQListHttpResponse extends ResponseMessage {

        private ArrayList<FAQListItem> FAQList = new ArrayList<FAQListItem>();

        public ArrayList<FAQListItem> getFAQList() {
            if (FAQList == null) {
                FAQList = new ArrayList<FAQListItem>();
            }
            return FAQList;
        }

        public void setFAQList(ArrayList<FAQListItem> FAQList) {
            this.FAQList = FAQList;
        }
    }

}
